﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Mail;

namespace Helper
{
    public class MailParameters
    {
        private string mMailServerName = ConfigurationManager.AppSettings["mailServerName"] == null ? string.Empty : ConfigurationManager.AppSettings["mailServerName"].ToString();
        /// <summary>
        /// E-mail Server name.
        /// If you pass an empty to this property, default server name is used which is written in app.config file as "mailServerName".
        /// </summary>
        public string MailServerName
        {
            get { return mMailServerName; }
            set { mMailServerName = value; }
        }

        private string mFrom = string.Empty;
        private string mTo = string.Empty;
        private string mCC = string.Empty;
        private string mBCC = string.Empty;
        private string mSubject = string.Empty;
        private string mBody = string.Empty;
        private MailPriority mPriority = MailPriority.Normal;
        private bool mIsBodyHtml = true;
        private MailAddressCollection mTOs = null;
        private MailAddressCollection mCCs = null;
        private MailAddressCollection mBCCs = null;
        private string mFromPassword = string.Empty;
        private bool mIsFromPasswordEncripted = false;
        private string mAttachment = string.Empty;
        private List<Attachment> mAttachments = null;

        /// <summary>
        /// Sender e-mail address.
        /// If you pass an empty to this property, default sender name is used which is written in app.config file as "mailServerUserName".
        /// </summary>
        public string From { get { return mFrom; } set { mFrom = value; } }
        /// <summary>
        /// Receiver e-mail address.
        /// If there is only one e-mail adress or if you want to send e-mail by parsing addresses, this property is used.
        /// 
        /// </summary>
        public string To { get { return mTo; } set { mTo = value; } }
        /// <summary>
        /// CC (Carbon Copy) e-mail address.
        /// If there is only one e-mail adress or if you want to send e-mail by parsing addresses, this property is used.
        /// </summary>
        public string CC { get { return mCC; } set { mCC = value; } }
        /// <summary>
        /// BCC (Blind Carbon Copy) e-mail address.
        /// If there is only one e-mail adress or if you want to send e-mail by parsing addresses, this property is used.
        /// </summary>
        public string BCC { get { return mBCC; } set { mBCC = value; } }
        /// <summary>
        /// e-mail Subject.
        /// </summary>
        public string Subject { get { return mSubject; } set { mSubject = value; } }
        /// <summary>
        /// e-mail Body
        /// </summary>
        public string Body { get { return mBody; } set { mBody = value; } }
        /// <summary>
        /// e-mail Priority. Low, Normal or High. Default value "Normal".
        /// </summary>
        public MailPriority Priority { get { return mPriority; } set { mPriority = value; } }
        /// <summary>
        /// Is sending e-mail in text format or in html format. Default value is "true".
        /// </summary>
        public bool IsBodyHtml { get { return mIsBodyHtml; } set { mIsBodyHtml = value; } }

        /// <summary>
        /// CC (Carbon Copy) e-mail address list. If more then one e-mail addresses will be defined, this property is used.
        /// </summary>
        public MailAddressCollection CCs { get { return mCCs; } set { mCCs = value; } }
        /// <summary>
        /// Bcc (Blind Carbon Copy) e-mail address list. If more then one e-mail addresses will be defined, this property is used.
        /// </summary>
        public MailAddressCollection BCCs { get { return mBCCs; } set { mBCCs = value; } }
        /// <summary>
        /// Receiver e-mail address list. If more then one e-mail addresses will be defined, this property is used.
        /// </summary>
        public MailAddressCollection TOs { get { return mTOs; } set { mTOs = value; } }

        /// <summary>
        /// e-mail sender password. 
        /// If you pass an empty to this property, default sender name is used which is written in app.config file as "mailServerUserPassword".
        /// </summary>
        public string FromPassword
        {
            get { return mFromPassword; }
            set { mFromPassword = value; }
        }
        /// <summary>
        /// Information of if sender password is encrypted or not. Default value is "false".
        /// </summary>
        public bool IsFromPasswordEncripted
        {
            get { return mIsFromPasswordEncripted; }
            set { mIsFromPasswordEncripted = value; }
        }

        public string Attachment { get { return mAttachment; } set { mAttachment = value; } }
        public List<Attachment> Attachments { get { return mAttachments; } set { mAttachments = value; } }
    }

    /// <summary>
    /// Used to send e-mail.
    /// MailServerName, MailServerUserName and MailServerUserPassword parameters are defined in app.config file.
    /// </summary>
    public static class MailHelper
    {
        private static string MailServerName
        {
            get { return ConfigurationManager.AppSettings["mailServerName"] == null ? string.Empty : ConfigurationManager.AppSettings["mailServerName"].ToString(); }
        }
        private static string MailServerUserName
        {
            get { return ConfigurationManager.AppSettings["mailServerUserName"] == null ? string.Empty : ConfigurationManager.AppSettings["mailServerUserName"].ToString(); }
        }
        private static string MailServerUserPassword
        {
            get { return ConfigurationManager.AppSettings["mailServerUserPassword"] == null ? string.Empty : ConfigurationManager.AppSettings["mailServerUserPassword"].ToString(); }
        }

        /// <summary>
        /// Used to send e-mail.
        /// </summary>
        /// <param name="pTo">Receiver e-mail address. It takes only one e-mail address.</param>
        /// <param name="pSubject">e-mail subject</param>
        /// <param name="pBody">e-mail body</param>
        public static void SendMail(string pTo, string pSubject, string pBody)
        {
            SendMail(pTo, pSubject, pBody, MailPriority.Normal, string.Empty);
        }
        /// <summary>
        /// Used to send e-mail.
        /// </summary>
        /// <param name="pTo">Receiver e-mail address. It takes only one e-mail address.</param>
        /// <param name="pSubject">e-mail subject</param>
        /// <param name="pBody">e-mail body</param>
        /// <param name="pPriority">e-mail Priority. Low, Normal or High.</param>
        public static void SendMail(string pTo, string pSubject, string pBody, MailPriority pPriority)
        {
            SendMail(pTo, pSubject, pBody, pPriority, string.Empty);
        }
        /// <summary>
        /// Used to send e-mail.
        /// </summary>
        /// <param name="pTo">Receiver e-mail address. It takes only one e-mail address.</param>
        /// <param name="pSubject">e-mail subject</param>
        /// <param name="pBody">e-mail body</param>
        /// <param name="pPriority">e-mail Priority. Low, Normal or High.</param>
        /// <param name="pAttachmentFileName">e-mail attachment file path with file name. e.g: C:\docs\file.docx</param>
        public static void SendMail(string pTo, string pSubject, string pBody, MailPriority pPriority, string pAttachmentFileName)
        {
            string from = MailServerUserName;
            MailMessage oMessage = new MailMessage(from, pTo, pSubject, pBody);
            oMessage.IsBodyHtml = true;
            oMessage.Priority = pPriority;
            if (!string.IsNullOrEmpty(pAttachmentFileName)) oMessage.Attachments.Add(new Attachment(pAttachmentFileName));

            SmtpClient mailClient = new SmtpClient();
            mailClient.Host = MailServerName;
            mailClient.Credentials = new NetworkCredential(MailServerUserName, MailServerUserPassword.Decrypt());

            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(oMessage);
        }

        /// <summary>
        /// Used to send e-mail.
        /// </summary>
        /// <param name="pParameters">e-mail parameters object as MailParameters class</param>
        public static void SendMail(MailParameters pParameters)
        {
            string mailServerUserName = MailServerUserName;
            string mailServerUserPassword = MailServerUserPassword;
            bool isPwEncripted = true;

            MailMessage oMessage = new MailMessage();

            if (!string.IsNullOrEmpty(pParameters.From))
            {
                oMessage.From = new MailAddress(pParameters.From);
                mailServerUserName = pParameters.From;
                mailServerUserPassword = pParameters.FromPassword;
                isPwEncripted = pParameters.IsFromPasswordEncripted;
            }
            else
            {
                oMessage.From = new MailAddress(mailServerUserName);
            }

            if (!string.IsNullOrEmpty(pParameters.To)) oMessage.To.Add(pParameters.To);
            if (!string.IsNullOrEmpty(pParameters.CC)) oMessage.CC.Add(pParameters.CC);
            if (!string.IsNullOrEmpty(pParameters.BCC)) oMessage.Bcc.Add(pParameters.BCC);
            if (pParameters.TOs != null)
            {
                foreach (MailAddress toAdres in pParameters.TOs)
                {
                    oMessage.To.Add(toAdres);
                }
            }
            if (pParameters.CCs != null)
            {
                foreach (MailAddress ccAdres in pParameters.CCs)
                {
                    oMessage.CC.Add(ccAdres);
                }
            }
            if (pParameters.BCCs != null)
            {
                foreach (MailAddress bccAdres in pParameters.BCCs)
                {
                    oMessage.Bcc.Add(bccAdres);
                }
            }

            oMessage.Subject = pParameters.Subject;
            oMessage.Body = pParameters.Body;
            oMessage.IsBodyHtml = pParameters.IsBodyHtml;
            oMessage.Priority = pParameters.Priority;

            if (!string.IsNullOrEmpty(pParameters.Attachment)) oMessage.Attachments.Add(new Attachment(pParameters.Attachment));
            if (pParameters.Attachments != null)
            {
                foreach (Attachment attachItem in pParameters.Attachments)
                {
                    oMessage.Attachments.Add(attachItem);
                }
            }

            SmtpClient mailClient = new SmtpClient();
            mailClient.Host = MailServerName;
            if (isPwEncripted)
                mailClient.Credentials = new NetworkCredential(mailServerUserName, mailServerUserPassword.Decrypt());
            else
                mailClient.Credentials = new NetworkCredential(mailServerUserName, mailServerUserPassword);

            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(oMessage);
        }

    }
}
